﻿namespace EyeSoft.Demo.FiscalCode.Wpf
{
	using System;
	using System.Globalization;
	using System.Windows.Data;

	public class StringToDateConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			var textValue = System.Convert.ToString(value);

			DateTime resultDateTime;

			if (DateTime.TryParse(textValue, out resultDateTime))
			{
				return resultDateTime;
			}

			return value;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			var textValue = System.Convert.ToString(value);

			DateTime resultDateTime;

			if (DateTime.TryParse(textValue, out resultDateTime))
			{
				return resultDateTime;
			}

			return value;
		}
	}
}